package com.flash.GlobalManagement.util;


import com.flash.GlobalManagement.mapper.DbutilMapper;
import com.flash.GlobalManagement.util.TableNameUtil;
import com.flash.GlobalManagement.util.TimeUtil;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 *
 */
@Service
public class Dbutil {
    @Resource
    DbutilMapper dbutilMapper;

    /**
     * 要日期的ID
     * @param tableId 表列ID的名字
     * @param table 表名
     * @return
     */
    public String getMaxId(String tableId,String table,String tableTime){
        //获取前缀
        String tableName = TableNameUtil.getTableName(table);

        //获取当前日期
        String time = TimeUtil.getTime(TimeUtil.type,"");

        String time2 = TimeUtil.getTime(TimeUtil.type2,"");
        //获取表的Id
        String tableIdMax = dbutilMapper.getTable(tableId,table,tableTime,time2);

        String substring;

        //获取表ID的最后3位数
        if(tableIdMax!=null){
            substring = tableIdMax.substring(tableIdMax.length() - 3);
        }else {
            substring ="1";
        }
        //将ID转换成int类型
        int i = Integer.parseInt(substring);

        if (substring=="1"){
            i=1;
        }else {
            i++;
        }


        //将算完后的数字转换成字符串
        String tableIdMaxOne = Integer.toString(i);

        //将前缀、日期、ID拼接起来
        StringBuffer tn = new StringBuffer(tableName);
        StringBuffer t = new StringBuffer(time);
        StringBuffer tio = new StringBuffer("00"+tableIdMaxOne);
        tn=tn.append(t);
        tn=tn.append(tio);

        String str = new String(tn);
        return str;
    }




    public String getMaxId3(String tableId,String table,String tableTime,String times){
        //获取前缀
        String tableName = TableNameUtil.getTableName(table);

        //获取选则日期
        String time = TimeUtil.getTime(TimeUtil.type,times);

        String time2 = TimeUtil.getTime(TimeUtil.type2,times);
        //获取表的Id
        String tableIdMax = dbutilMapper.getTable(tableId,table,tableTime,time2);

        String substring;

        //获取表ID的最后3位数
        if(tableIdMax!=null){
            substring = tableIdMax.substring(tableIdMax.length() - 3);
        }else {
            substring ="1";
        }
        //将ID转换成int类型
        int i = Integer.parseInt(substring);

        if (substring=="1"){
            i=1;
        }else {
            i++;
        }


        //将算完后的数字转换成字符串
        String tableIdMaxOne = Integer.toString(i);

        //将前缀、日期、ID拼接起来
        StringBuffer tn = new StringBuffer(tableName);
        StringBuffer t = new StringBuffer(time);
        StringBuffer tio = new StringBuffer("00"+tableIdMaxOne);
        tn=tn.append(t);
        tn=tn.append(tio);

        String str = new String(tn);
        return str;
    }

    /**
     * 不要日期的ID
     * @param tableId 表列ID的名字
     * @param table 表名
     * @return
     */
    public String getMaxId2(String tableId,String table){
        //获取前缀
        String tableName = TableNameUtil.getTableName(table);

        //获取表的Id
        String tableIdMax = dbutilMapper.getTable2(tableId, table);

        String substring;
        //获取表ID的最后4位数
        if(tableIdMax==null){
            substring="0";
        }else {
            substring = tableIdMax.substring(tableIdMax.length() - 3);
        }
        System.out.println("substring="+substring);
        //将ID转换成int类型
        int i = Integer.parseInt(substring);
        if(i==0){
            i=0001;
        }else {
            i++;
            System.out.println(i);
        }
        System.out.println("i="+i);
        //将算完后的数字转换成字符串
        String tableIdMaxOne = Integer.toString(i);

        //将前缀、ID拼接起来
        StringBuffer tn = new StringBuffer(tableName);
        StringBuffer tio = new StringBuffer("00"+tableIdMaxOne);
        System.out.println(tio);
        tn=tn.append(tio);

        String str = new String(tn);
        return str;
    }

    public static void main(String[] args) {
       /* int i = Integer.parseInt("asdfasd");
        System.out.println(i);*/
    }
}
